package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.JwtTokenUtils;
import com.example.entity.Admin;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class AdminService {

    @Resource
    private AdminMapper adminMapper;
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
    private static final String PHONE_REGEX = "^\\d{11}$"; // 正则表达式：11位数字

    private boolean isValidPhone(String phone) {
        return Pattern.matches(PHONE_REGEX, phone);
    }

    private boolean isValidEmail(String email) {
        return Pattern.matches(EMAIL_REGEX, email);
    }

    public void add(Admin admin) {
        if (ObjectUtil.isEmpty(admin.getPassword())) {
            admin.setPassword("123456");
        }

        // 校验电话是否为11位
        if (!isValidPhone(admin.getPhone())) {
            throw new CustomException("电话号码必须是11位数字");
        }

        // 校验邮箱格式
        if (!isValidEmail(admin.getEmail())) {
            throw new CustomException("邮箱格式不正确");
        }
        admin.setState(0);
        adminMapper.insert(admin);
    }

    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize, Admin admin) {
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    public void update(Admin admin) {
        // 校验电话是否为11位
        if (!isValidPhone(admin.getPhone())) {
            throw new CustomException("电话号码必须是11位数字");
        }

        // 校验邮箱格式
        if (!isValidEmail(admin.getEmail())) {
            throw new CustomException("邮箱格式不正确");
        }

        adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public Admin login(Admin admin) {
        Admin dbAdmin = adminMapper.selectByUsernameAndPassword(admin);
        if (ObjectUtil.isEmpty(dbAdmin)) {
            throw new CustomException("您输入的用户名或密码不正确");
        }

        // 检查 state 的值
        Integer state = dbAdmin.getState();
        if (state != null) {
            switch (state) {
                case 0:
                    throw new CustomException("账号未审核，请等待管理员审核");
                case 2:
                    throw new CustomException("账号审核未通过");
                case 1:
                    // 状态为 1 时允许登录，生成 token
                    String token = JwtTokenUtils.genToken(admin.getUsername(), admin.getPassword());
                    dbAdmin.setToken(token);
                    return dbAdmin;
                default:
                    throw new CustomException("账号状态异常，请联系管理员");
            }
        } else {
            throw new CustomException("账号状态未知，请联系管理员");
        }
    }

    public void updatePassword(Admin admin) {
        // 校验一下两次密码是否一致
        if (!admin.getNewPassword().equals(admin.getNew2Password())) {
            throw new CustomException("两次输入的密码不一致，请重新输入");
        }
        // 把密码改成新的密码
        admin.setPassword(admin.getNewPassword());
        adminMapper.updateById(admin);
    }

    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }
}
